<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: 当燃 2016-01-09
 */ 
namespace Mobile\Controller;
class IndexController extends MobileBaseController {

    public function index(){


        //首页店铺类型图标 及购买流程引到图
        $config = tpCache('index_img');
        /**
         *   各类店铺首页推荐
         */
        $goods_lists=array();
        $where_goods['seller_check']=1;
        $where_goods['status']=1;
        $order=' is_recommend DESC';//推荐的优先
        //天猫  goods_type=3
        $where_goods['goods_type']=3;
        $goods_lists['tm']=M('goods')->where($where_goods)->order($order)->limit(4)->select();
        //淘宝 goods_type=4
        $where_goods['goods_type']=4;
        $goods_lists['tb'] =M('goods')->where($where_goods)->order($order)->limit(4)->select();
        //京东 goods_type=5
        $where_goods['goods_type']=5;
        $goods_lists['jd'] =M('goods')->where($where_goods)->order($order)->limit(4)->select();
        //其他 goods_type=6
        $where_goods['goods_type']=6;
        $goods_lists['qt'] =M('goods')->where($where_goods)->order($order)->limit(4)->select();



        //查询成功案例
        $where['status']=1;
        $case_list=M('case')->where($where)->order('id DESC')->limit(6)->select();
        foreach($case_list AS $k=>$v){
            $v['goods']=M('goods')->where(array('goods_id'=>$v['goods_id']))->find();
            $case_list[$k]=$v;
        }


        //找不到店铺的时候 的客服QQ
        $shop_info_config = tpCache('shop_info');
        $this->assign('kf_qq',$shop_info_config['qq']);



        //首页弹窗提示信息

		$navid = "index";
		$this->assign('navid',$navid);
        $this->assign('config',$config);
        $this->assign('case_list',$case_list);


		
        $this->assign('goods_lists',$goods_lists);
        $this->assign('navid','index');
        $this->display();
    }

    //使用协议
    public function protocol(){
        $msg = M('article')->where(array('article_id'=>53))->find();
        $remess["msg"] = $msg["content"];
        $json = json_encode($remess);
        echo $json;
    }

    //提交表单
    public function seller_form(){
		$navid = "seller";
		$this->assign('navid',$navid);
        $this->display();
    }

    /**
     * 客户买卖需求
     */

    public function sub_need(){
        $data=I();
        if($_SESSION['user']['user_id']){
            $data['user_id']=$_SESSION['user']['user_id'];
        }

        if(!$data['name'])
            $this->error('名称不能为空！',U('Home/Index/index'));
        if(!$data['mobile'])
            $this->error('手机号不能为空！',U('Home/Index/index'));
        if(!$data['content'])
            $this->error('需求内容不能为空！',U('Home/Index/index'));

        $data['add_time']=time();
        $data['status']=0;
        $res=M('ask_buy')->add($data);
        if($res){
            $config = tpCache('shop_info');
            $to=$config['admin_email'];
            $title='新需求通知';
            $content=get_email_content_ask($res);
            new_send_email($to,$title,$content);
            $this->success('提交成功，客服人员将尽快与您联系。',U('Home/Index/index'));
        }else{
            $this->error('提交失败，请重试！',U('Home/Index/index'));
        }
    }

    public function seller_from(){
        $this->display();
    }




    public function set_kf_cookie(){
        $kf_hide=I('kf_hide');
        cookie('kf_hide',$kf_hide);
    }
    public function index_old(){
        /*$lang = L('_LANG_');
        $this->assign('wellcome', $lang);*/
        // 如果是手机跳转到 手机模块
        if(true == isMobile()){
            header("Location: ".U('Mobile/Index/index'));
        }
        $hot_goods = $hot_cate = $cateList = array();
        $sql = "select * from __PREFIX__goods as a left join ";
        //        $sql .= " __PREFIX__goods_category as b on a.cat_id=b.id where a.is_hot=1 and a.is_on_sale=1 order by a.sort";//二级分类下热卖商品
        $sql .= " __PREFIX__goods_category as b on a.cat_id=b.id where  1=1  ";//二级分类下热卖商品
        //        $index_hot_goods = M()->query($sql);//首页热卖商品
        //		if($index_hot_goods){
        //			foreach($index_hot_goods as $val){
        //				$cat_path = explode('_', $val['parent_id_path']);
        //				$hot_goods[$cat_path[1]][] = $val;
        //			}
        //		}
        $hot_category = M('goods_category')->where("is_hot=1 and level=3 and is_show=1")->select();//热门三级分类
        foreach ($hot_category as $v){
            $cat_path = explode('_', $v['parent_id_path']);
            $hot_cate[$cat_path[1]][] = $v;
        }

        foreach ($this->cateTrre as $k=>$v){
            if($v['is_hot']==1){
                $v['hot_goods'] = empty($hot_goods[$k]) ? '' : $hot_goods[$k];
                $v['hot_cate'] = empty($hot_cate[$k]) ? '' : $hot_cate[$k];
                $cateList[] = $v;
            }
        }

        $this->assign('cateList',$cateList);
        $this->display();
    }



    /**
     *  公告详情页
     */
    public function notice(){
        $this->display();
    }

    public function ajaxHideTop()
    {
        // $ids = I("ids"); // 商品 ids
        // $result = M("Cart")->where(" id in ($ids)")->delete(); // 删除id为5的用户数据
        if($_COOKIE["hidetopstatus"]=="show")
        {
            //cookie('hidetopstatus','null',time() - 3600);
            //unset($_COOKIE['hidetopstatus']);
            //cookie('hidetopstatus','asdkfjsadlfadsf',time() - 3600);
            cookie('hidetopstatus','hide',3600);
            $return_arr = array('status'=>2,'msg'=>'','result'=>''); // 返回结果状态
        }
        else
        {
            cookie('hidetopstatus','show',3600);
            $return_arr = array('status'=>1,'msg'=>'','result'=>''); // 返回结果状态
        }
        exit(json_encode($return_arr));
    }

    // 二维码
    public function qr_code(){
        // 导入Vendor类库包 Library/Vendor/Zend/Server.class.php
        //http://www.tp-shop.cn/Home/Index/erweima/data/www.99soubao.com
        require_once 'ThinkPHP/Library/Vendor/phpqrcode/phpqrcode.php';
        //import('Vendor.phpqrcode.phpqrcode');
        error_reporting(E_ERROR);
        $url = urldecode($_GET["data"]);
        \QRcode::png($url);
    }

    // 验证码
    public function verify()
    {
        //验证码类型
        $type = I('get.type') ? I('get.type') : '';
        $fontSize = I('get.fontSize') ? I('get.fontSize') : '40';
        $length = I('get.length') ? I('get.length') : '4';

        $config = array(
            'fontSize' => $fontSize,
            'length' => $length,
            'useCurve' => true,
            'useNoise' => false,
        );
        $Verify = new Verify($config);
        $Verify->entry($type);
    }

    // 促销活动页面
    public function promoteList()
    {
        $Model = new \Think\Model();
        $goodsList = $Model->query("select * from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id   where ".time()." > start_time  and ".time()." < end_time");
        $brandList = M('brand')->getField("id,name,logo");
        $this->assign('brandList',$brandList);
        $this->assign('goodsList',$goodsList);
        $this->display();
    }

    function truncate_tables (){
        $model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
        $tables = $model->query("show tables");
        $table = array('tp_admin','tp_config','tp_region','tp_system_module','tp_admin_role','tp_system_menu');
        foreach($tables as $key => $val)
        {
            if(!in_array($val['tables_in_tpshop'], $table))
                echo "truncate table ".$val['tables_in_tpshop'].' ; ';
            echo "<br/>";
        }
    }
}